import com.baizhi.entity.LuceneTest;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

import javax.xml.crypto.Data;
import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * @Author: renpu
 * @Date: 2019/3/12 14:19
 * @Description:
 */

public class Lucence {

    @Test
    public void test1() throws IOException {
        //准备数据；
        LuceneTest luceneTest=new LuceneTest("和班长一起同居的日子","吴奇隆",new Date(),"领导风格经历的守法公民第三方过后挂号费");


        //创建Document对象；
        Document document=new Document();

        //将实体类的对象封装到Documet对象中；
        document.add(new TextField("title",luceneTest.getTiltle(), Field.Store.YES));
        document.add(new StringField("author",luceneTest.getAuthor(), Field.Store.YES));
        document.add(new TextField("content",luceneTest.getContent(), Field.Store.YES));

        //创建分词器对象，对数据的词进行分类建立索引
       /* StandardAnalyzer standardAnalyzer=new StandardAnalyzer(Version.LUCENE_44);*/
        Analyzer standardAnalyzer=new SmartChineseAnalyzer(Version.LUCENE_44);

        //创建FSDirectory用来创建索引库的具体路径；
        FSDirectory fsDirectory=FSDirectory.open(new File("D://index"));

        //创建IndexWriterConfig对象，配置Lucene的写入索引库的配置；
        IndexWriterConfig indexWriterConfig=new IndexWriterConfig(Version.LUCENE_44,standardAnalyzer);

        //创建索引写对象
        IndexWriter indexWriter = new IndexWriter(fsDirectory,indexWriterConfig);

        //将整个文件写入索引库；
        indexWriter.addDocument(document);

        //将此次操作提交事务；
        indexWriter.commit();

        //释放资源，未释放资源会导致产生写操作的锁，当大量数据写入时会导致一些资源的异常；
        indexWriter.close();
    }

    @Test
    public void Query() throws Exception {


        //创建查询的对象MutiFieldQueryParser；

        //准备查询的数据；
        String[] files={"title","author","content"};
        //准备查询时所需的分词器以及lucene的版本；
       /* StandardAnalyzer standardAnalyzer = new StandardAnalyzer(Version.LUCENE_44);*/
        Analyzer standardAnalyzer=new SmartChineseAnalyzer(Version.LUCENE_44);
        MultiFieldQueryParser multiFieldQueryParser=new MultiFieldQueryParser(Version.LUCENE_44, files, standardAnalyzer);


        //创建查询Query对象，通过multiFieldQuery调用parse对象；
        Query query = multiFieldQueryParser.parse("班长");

        //指定根据查询的数据去哪个索引库进行相应的查询以及匹配，创建FSDirectory对象获取索引库的具体路径；
        FSDirectory fsDirectory=FSDirectory.open(new File("D://index"));

        //获取文件读取对象，DirectoryReader;
        DirectoryReader reader=DirectoryReader.open(fsDirectory);

        //将读取对象从索引读取的数据，交给查询对象IndexSeacher进行查询;
        IndexSearcher searcher=new IndexSearcher(reader);

        //设定查询的关键字，以及查询相关的范围
        TopDocs search = searcher.search(query, 100);

        //获取到从索引库查询的分词对应的数据位置；
        ScoreDoc[] scoreDocs = search.scoreDocs;
        for (int i=0;i<scoreDocs.length;i++){

            //查看与要查询的关键词的相关度；
            float score = scoreDocs[i].score;

            System.out.println("分数"+score);

            //根据索引库分词对应的数据位置，对应的索引值
            int doc = scoreDocs[i].doc;

            //去查询将值同时也赋值给Document对象；
            Document document = searcher.doc(doc);

            //获取每个标题对应的数据；
            String title=document.get("title");
            String author=document.get("author");
            String content=document.get("content");


            //封装到实体类；
            LuceneTest luceneTest = new LuceneTest(title,author,new Date(),content);

            System.out.println(luceneTest);

        }

    }

}
